<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Stict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <link rel="stylesheet" type="text/css" href="../../css/webuploader.css">


</head>



<body>
<div class="layuimini-container">
    <div class="layuimini-main">


        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>
        <script type="text/html" id="currentTableBar">
        </script>

    </div>
</div>

<style type="text/css">
    .layui-table-cell{
        text-align:center;

        height: auto;

        white-space: normal;

    }

    .layui-table img{
        max-width:250px
    }
</style>


<script type="text/html" id="status">
    {{#  if(d.isSubmit === false ){ }}
    <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-danger">未提交</button>
    {{#  } }}
    {{#  if(d.isSubmit === true ){ }}
    <button class="layui-btn layui-btn-sm layui-btn-radius layui-btn-normal">已提交</button>
    {{#  } }}
</script>
<script src="../../lib/layui-v2.7.3/layui/layui.js"></script>
<script src="../../js/lay-config.js?v=1.0.4" charset="utf-8"></script>
<script src="../../lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="../../api/urlConfig.js"></script>
<script src="../../js/base64.min.js"></script>
<script>

    var maxSingleFileSize;
    $(function () {
        $.ajax({
            url: ipURL + '/admin/system',
            type: 'GET',
            dataType: 'json',
            beforeSend: function (request) {
                request.setRequestHeader("token", localStorage.token);
            },
            success: function (data) {
                maxSingleFileSize = data.data.data.requestFileSize * 1024
            },
        });
    })

    var tid;
    function child(obj) {
        tid = obj.id
        var upViewIndex = null;
        layui.use(['form', 'table','upload','dropdown'], function () {
            var upload = layui.upload;
            var $ = layui.jquery,
                form = layui.form,
                table = layui.table,
                dropdown = layui.dropdown
            ;

            // 监听搜索操作
            form.on('submit(data-search-btn)', function (data) {

                var userName = $('input[name="userName"]').val()
                var email = $('input[name="studentNumber"]').val()
                //执行搜索重载
                table.reload('currentTableId', {

                    page: {
                        curr: 1
                    },
                    where: {
                        userName: userName,
                        email:email
                    }
                });

                return false;
            });


            table.render({
                elem: '#currentTableId',
                // url: '../api/table.json',
                url: ipURL + "/user/task/get/" + obj.id,
                headers: {token: localStorage.token},
                parseData: function (res) { //res 即为原始返回的数据

                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.count, //解析数据长度
                        "data": res.data.data, //解析数据列表

                    };

                },

                response: {
                    statusCode: 0 //规定成功的状态码，默认：0
                },
                text: {
                    none: '无数据'
                },
                toolbar: '#toolbarDemo',
                defaultToolbar: ['filter', 'exports', 'print', {
                    title: '提示',
                    layEvent: 'LAYTABLE_TIPS',
                    icon: 'layui-icon-tips'
                }],
                cols: [
                    [
                        {
                            field: 'isSubmit',
                            title: '状态',
                            templet: '#status',
                            sort: true
                        },
                        {
                            title: '文件名',
                            field: 'fileName',
                        },
                        {
                            field: 'fileSizeString',
                            title: '文件大小',
                        },
                        {
                            field: 'praFileName',
                            title: '最终文件名',
                        },
                        {
                            field: 'submitTime',
                            title: '提交时间',
                            width: 150,
                        },
                        {
                            title: '操作',
                            minWidth: 300,
                            align: "center",
                            templet: function(d){
                                var operation = '';
                                if(d.filePath != ''){
                                    operation +=  `<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="download">下载</a>`;
                                    operation+=" "
                                    operation +=  `<a class="layui-btn layui-btn-xs layui-btn-warm" lay-event="show">预览</a>`;
                                    operation+=" "
                                    operation +=   `<a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">撤回</a>`;
                                    operation+=" "
                                }
                                operation+= `<a class='layui-btn layui-btn-normal layui-btn-xs data-count-edit upload_btn' value="${d.id}" lay-event='scbb'>上传文件</a>`;

                                return operation
                            }
            }
                    ]
                ],
                done:function(res, curr, count){
                    var tableId = this.id;
                    var tableElem = this.elem;
                    //table render出来实际显示的table组件
                    var tableViewElem = tableElem.next();
                    //渲染当前页面的所有upload组件
                    var taskId
                    upload.render({
                        elem: tableViewElem.find('.upload_btn')
                        ,url: ipURL+"/user/task/upload"
                        ,accept: 'file'
                        ,size: maxSingleFileSize
                        ,headers: {token: localStorage.token}
                        ,before: function(obj){ //obj包含信息，跟choose回调完全一致。在传值之前先获取id
                            var tableElem = this.item;
                            taskId = tableElem.attr('value');
                            layer.load(); //上传loading
                        }
                        ,data: {
                            taskRecordId:function(){
                                return taskId
                            },
                        }
                        ,done: function (res) {
                            if (res.state){
                                layer.closeAll('loading'); //关闭loading
                                layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                            }else{
                                layer.closeAll('loading'); //关闭loading
                            }
                            layer.msg(res.message)

                        }
                        ,error: function(index, upload){
                        }
                    });
                },
                limits: [10, 15, 20, 25, 50, 100],
                limit: 15,
                page: true,
                skin: 'line'
            });


            table.on('tool(currentTableFilter)', function (obj) {
                var that = this;
                var data = obj.data;

                if(obj.event === 'delete'){
                    $.ajax({
                        url: ipURL + '/user/task/sendBack/'+data.id,
                        method: 'delete',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function (res) {
                            layer.msg(res.message);
                            if (res.state){
                                layui.table.reload('currentTableId'); //重载父页表格，参数为表格ID
                            }

                        },
                        error: function (data) {
                            layer.msg(data.message);
                        }
                    });
                }else if(obj.event === 'show'){
                    var filePath = data.filePath; //要预览文件的访问地址
                    if (filePath === ''){
                        layer.msg("文件路径为空")
                        return;
                    }
                    window.open('http://your remote IP:8012/onlinePreview?url='+filePath);
                    // window.open('http://127.0.0.1:8012/onlinePreview?url='+filePath);
                }else if(obj.event === 'download'){

                    $.ajax({
                        url: ipURL + '/common/file/download/'+data.id,
                        method: 'get',
                        dataType: 'JSON',
                        contentType: 'application/json;charset=UTF-8',
                        beforeSend: function (request) {
                            request.setRequestHeader("token", localStorage.token);
                        },
                        success: function (res) {
                            layer.msg(res.message)
                            if (res.state){
                                const a = document.createElement('a');
                                a.download =data.fileName;
                                a.href =res.data.data;
                                document.documentElement.appendChild(a);
                                // 先判断是否为Firefox浏览器,火狐会下载两次
                                const isFirefox = typeof InstallTrigger !== 'undefined';
                                if (isFirefox) {
                                    setTimeout(() => {
                                        a.click();
                                        a.remove();
                                    }, 200);
                                } else {
                                    a.click();
                                    a.remove();
                                }
                            }

                        },
                        error: function (data) {
                            layer.msg(data.message);
                        }
                    });

                }
            });

        });

    };


</script>

</body>
</html>
